home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1998 May
/
Macworld (1998-05).dmg
/
Serious Demos
/
Lasso 2.5 Test Drive
/
Lasso 2.5 CGI
/
Java
/
LassoProxy
/
LassoResponse.java
< prev
next >
Wrap
Text File
|
1997-12-12
|
10KB
|
389 lines
/*
LassoResponse.java
Response parameter from LassoProxy. While not currently enforced, all public attributes
should be treated as read-only.
Lasso responses are normally created by LassoProxy methods; you should never have any
need to instatiate a LassoResponse directly yourself.
The toString() method displays the raw response string returned from Lasso. This can be
useful for debugging.
Copyright © 1996 Blue World Communications, Inc.. All rights reserved.
*/
import java.util.*;
import java.net.*;
public class LassoResponse
{
// the various data types for fields
// one of these will be returned from the getFieldType method
public static final int NONE = 0;
public static final int CHAR = 1;
public static final int SHORT_INTEGER = 2;
public static final int INTEGER = 3;
public static final int SHORT_FLOAT = 4;
public static final int FLOAT = 5;
public static final int IMAGE = 6;
public static final int DATE_TIME = 7;
public static final int BOOLEAN = 8;
// result codes
public static final int NOERROR = 0;
public static final int CONNECTION_INVALID = -609; // the database application is not open
public static final int TIMEOUT = -1712; // the request timed out
public static final int BAD_DATA_SUPPLIED = -17005; // for instance, an invalid date for a date field
public static final int REQUIRED_FIELD_MISSING = -800; // data was not supplied or a required field
public static final int MEMORY_FULL = -108; // the server ran out of memory processing the request
public static final int NO_SUCH_OBJECT = -1728; // either the database or the layout do not exist
// or no records were found
private int fNumFields; // number of fields in the layout
private String fFieldNames[]; // field names
private boolean fFieldRepeats[]; // true = repeating field
private int fRepeatSize[]; // number of values allowed for repeating field
private boolean fFieldNullsOK[]; // allowed to be blank when adding a new record
private char fFieldType[]; // default field type
private String fValueList[][]; // value list for the field for the layout
private int fNumFound; // number of records in the result set
private int fNumRecords; // number of records retrieved
private int fNumTotal; // total number of records in the database
private int fRecordID[]; // IDs of retrieved records
private int fResultCode; // the result. 0 (zero) is no error
private String fFieldValue[][][]; // indexed by: record, field, field value (multiple)
private String fResponse;
public LassoResponse( String lassoResponse )
{
String temp;
StringTokenizer tempTokenizer;
Integer tempInt;
fResponse = lassoResponse;
StringTokenizer tokenizer = new StringTokenizer( lassoResponse, "\u000b" );
fNumFields = 0;
fResultCode = 0;
temp = tokenizer.nextToken();
tempTokenizer = new StringTokenizer( temp, "=" );
temp = tempTokenizer.nextToken();
if (temp.equals("[resultcode]"))
{
tempInt = new Integer(tempTokenizer.nextToken());
fResultCode = tempInt.intValue();
temp = tokenizer.nextToken();
tempTokenizer = new StringTokenizer( temp, "=" );
temp = tempTokenizer.nextToken();
}
if ( temp.equals( "[numfields]" ) )
{
tempInt = new Integer( tempTokenizer.nextToken() );
fNumFields = tempInt.intValue();
}
if ( fNumFields <= 0 ) return;
fFieldNames = new String[ fNumFields ];
for ( int i = 0; i < fNumFields; i++ )
{
fFieldNames[ i ] = tokenizer.nextToken();
}
temp = tokenizer.nextToken(); // repeats flags and repeat sizes
tempTokenizer = new StringTokenizer( temp, "ft", true );
fFieldRepeats = new boolean[ fNumFields ];
fRepeatSize = new int[ fNumFields ];
for ( int i = 0; ( i < fNumFields ) && tempTokenizer.hasMoreTokens(); i++ )
{
temp = tempTokenizer.nextToken();
if ( temp.equals( "t" ) )
{
tempInt = new Integer( tempTokenizer.nextToken() );
fFieldRepeats[ i ] = true;
fRepeatSize[ i ] = tempInt.intValue();
}
else
{
fFieldRepeats[ i ] = false;
fRepeatSize[ i ] = 0;
}
}
temp = tokenizer.nextToken(); // nulls OK flags
tempTokenizer = new StringTokenizer( temp, "ft", true );
fFieldNullsOK = new boolean[ fNumFields ];
for ( int i = 0; ( i < fNumFields ) && tempTokenizer.hasMoreTokens(); i++ )
{
temp = tempTokenizer.nextToken();
if ( temp.equals( "t" ) )
{
fFieldNullsOK[ i ] = true;
}
else
{
fFieldNullsOK[ i ] = false;
}
}
temp = tokenizer.nextToken(); // default type flags
fFieldType = new char[ fNumFields ];
for ( int i = 0; i < fNumFields; i++ )
{
fFieldType[ i ] = temp.charAt( i );
}
// extract value lists
fValueList = new String[ fNumFields ][];
for ( int i = 0; i < fNumFields; i++ )
{
tempInt = new Integer( tokenizer.nextToken() ); // number of list values
int numValues = tempInt.intValue();
if ( numValues > 0 )
{
fValueList[ i ] = new String[ numValues ];
for ( int j = 0; j < numValues; j++ )
{
fValueList[ i ][ j ] = tokenizer.nextToken();
}
}
}
// extract record data
fNumRecords = 0;
temp = tokenizer.nextToken();
tempTokenizer = new StringTokenizer( temp, "=" );
temp = tempTokenizer.nextToken();
if ( temp.equals( "[numreturned]" ) )
{
tempInt = new Integer( tempTokenizer.nextToken() );
fNumRecords = tempInt.intValue();
}
if ( fNumRecords <= 0 ) return;
fRecordID = new int[ fNumRecords ];
for ( int i = 0; i < fNumRecords; i++ )
{
tempInt = new Integer( tokenizer.nextToken() );
fRecordID[ i ] = tempInt.intValue();
}
fFieldValue = new String[ fNumRecords ][ fNumFields ][];
for ( int i = 0; i < fNumRecords; i++ )
{
for ( int j = 0; j < fNumFields; j++ )
{
temp = tokenizer.nextToken();
// temp contains field value; multiple values, such as for value lists, relational
// fields and repeating fields, are separated by the character "\u000c"; a null
// value is indicated by the character "\u0007"
tempTokenizer = new StringTokenizer( temp, "\u000c" );
int numValues = tempTokenizer.countTokens();
fFieldValue[ i ][ j ] = new String[ numValues ];
for ( int k = 0; k < numValues; k++ )
{
temp = tempTokenizer.nextToken();
if ( temp.charAt( 0 ) == '\u0007' ) // null value
{
fFieldValue[ i ][ j ][ k ] = new String(); // empty String
}
else
{
fFieldValue[ i ][ j ][ k ] = temp;
}
}
}
}
fNumFound = 0;
temp = tokenizer.nextToken();
tempTokenizer = new StringTokenizer( temp, "=" );
temp = tempTokenizer.nextToken();
if ( temp.equals( "[numfound]" ) )
{
tempInt = new Integer( tempTokenizer.nextToken() );
fNumFound = tempInt.intValue();
}
fNumTotal = 0;
temp = tokenizer.nextToken();
tempTokenizer = new StringTokenizer( temp, "=" );
temp = tempTokenizer.nextToken();
if ( temp.equals( "[numtotal]" ) )
{
tempInt = new Integer( tempTokenizer.nextToken() );
fNumTotal = tempInt.intValue();
}
}
// returns all the fields and their values for a particular record
public final String[][] recordData(int i)
{
return fFieldValue[i];
}
// returns all the values for a particular field of a particular record
public final String[] fieldData(int recordNum, int fieldNum)
{
return recordData(recordNum)[fieldNum];
}
// returns a particular value, of a particular field, of a particular record
public final String fieldValue(int recordNum, int fieldNum, int valueNum)
{
return fieldData(recordNum, fieldNum)[valueNum];
}
// returns the total number of records found
// not to be confused with numRecords()
// which returns the number of records retreived
public int numFound()
{ return fNumFound; }
// the number of records which were retreived
public int numRecords()
{ return fNumRecords; }
// total number of records in the database
public int numTotal()
{ return fNumTotal; }
// returns the id of the specified record
public int recordID(int i)
{ return fRecordID[i]; }
// returns the number of fields in the selected layout
public int numFields()
{ return fNumFields; }
// returns all the field names
public String[] fieldNames()
{ return fFieldNames; }
// returns the name of a particular field
public String fieldName(int i)
{ return fFieldNames[i]; }
// returns true if a particular field repeats
public boolean fieldRepeats(int i)
{ return fFieldRepeats[i]; }
// returns the maxumun number of repeat values for a particular field
public int repeatSize(int i)
{ return fRepeatSize[i]; }
// returns true of false depending on if the field can be blank
public boolean fieldNullsOK(int i)
{ return fFieldNullsOK[i]; }
// returns the value list associated with a particular field
public String[] fieldValueList(int i)
{ return fValueList[i]; }
// returns the result code for this response 0 (zero) is no error
public int resultCode()
{ return fResultCode; }
public int fieldIndex( String fieldName ) throws FieldNotFoundException
{
for ( int i = 0; i < fNumFields; i++ )
{
if ( this.fFieldNames[ i ].equals( fieldName ) )
return i;
}
throw ( new FieldNotFoundException( fieldName ) );
}
public boolean hasValueList( String fieldName )
{
try
{
int fieldIndex = fieldIndex( fieldName );
return ( ( fValueList[ fieldIndex ] != null ) &&
( fValueList[ fieldIndex ].length > 0 ) );
}
catch( FieldNotFoundException e )
{
System.out.println( "Exception: " + e );
return false;
}
}
public int fieldType(int index)
{
switch(fFieldType[index])
{
case 'b': return BOOLEAN;
case 'c': return CHAR;
case 's': return SHORT_INTEGER;
case 'l': return INTEGER;
case 'f': return SHORT_FLOAT;
case 'd': return FLOAT;
case 'p': return IMAGE;
case 't': return DATE_TIME;
}
return NONE;
}
public String toString()
{
return fResponse;
}
}
class FieldNotFoundException extends Exception
{
private String fieldName;
public FieldNotFoundException( String theName )
{
fieldName = theName;
}
public String toString()
{
return ( "Field Not Found: " + fieldName );
}
}